<?php
namespace app\model\question;

use think\Model;
use app\model\user\ZxlTalentUserModel;

class ZxlKnowStoreQuestionNoteModel extends Model
{
    protected $name = 'zxl_know_store_question_note';

    /**
     * 关联查询用户信息
     * 
     * @return array
     */
    public function user(){
        return $this->hasOne(ZxlTalentUserModel::class, "id", "user_id")->field("id, nickname_two, avatar, mobile");
    }

    /**
     * 我的所有笔记 以及 其他人的公开笔记
     * 
     * @return array
     */
    public static function getNoteList($question_id, $user_id, $limit, $page) {
        $where = function($q)use($user_id){
            $q->whereOr('user_id', $user_id)
            ->whereOr('is_public', 1);
        };
        $list = self::with(['user'])->where('question_id', $question_id)->where($where)->order(['create_time' => 'desc'])
        ->paginate($limit,false,['page'=>$page])->toArray();
        foreach ($list['data'] as &$v) {
            $v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            $v['is_like'] = ZxlKnowStoreQuestionNoteLikeLogModel::isLike($v['id'], $user_id);
            $v['self'] = 0;
            if ($v['user_id'] == $user_id) {
                $v['self'] = 1;
            }
        }
        return $list;
    }
}